<template>
  <BasicDrawer v-bind="$attrs" @register="registerDrawer" :title="getTitle" width="750" @ok="handleSubmit" destroyOnClose>
    <BasicForm @register="registerForm">
      <!-- <template #accessSecret="{ model, field, schema }">
        <a-row :gutter="8">
          <a-col :sm="16" :md="16" :lg="17" :xl="19">
            <ApiSelect         
              v-model:value="model[field]"
              optionFilterProp="label"
              resultField="list"
              v-bind="schema.componentProps"
            />
          </a-col>
          <a-col :sm="8" :md="7" :lg="7" :xl="5">
            <a-button type="primary" style="width: 100%" >管理</a-button>
          </a-col>
        </a-row>
      </template> -->
    </BasicForm>
  </BasicDrawer>
</template>
<script lang="ts" setup>
  import { ref, computed, unref, useAttrs } from "vue"
  import { BasicForm, useForm } from "/@/components/Form/index"
  import { formSchema } from "./mchApp.data"
  import { BasicDrawer, useDrawerInner } from "/@/components/Drawer"
  import { saveOrUpdateMchApp, getMchApp } from "/@/api/pay"
  import { useMessage } from "/@/hooks/web/useMessage"
  import { ApiSelect } from "/@/components/Form/index"
  const { createMessage } = useMessage()
  // 声明Emits
  const emit = defineEmits(["success", "register"])
  const attrs = useAttrs()
  const isUpdate = ref(true)
  const rowId = ref("")
  //表单配置
  const [registerForm, { setProps, resetFields, setFieldsValue, validate, updateSchema }] = useForm({
    labelWidth: 100,
    wrapperCol: {
      md: { span: 22 },
      sm: { span: 22 },
    },
    schemas: formSchema,
    showActionButtonGroup: false,
  })

  //表单赋值
  const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
    await resetFields()
    let showFooter = data?.showFooter ?? true
    setDrawerProps({ confirmLoading: false, showFooter })
    isUpdate.value = !!data?.isUpdate
    let mchInfo: any = {}
    if (unref(isUpdate)) {
      rowId.value = data.record.mchNo
      mchInfo = await getMchApp({ id: data.record.id })
    } else {
      mchInfo = { ...mchInfo, mchNo: data.mchNo }
    }
    updateSchema([
      {
        field: "appSecret",
        label: "应用私钥",
        required: !isUpdate.value,
        componentProps: {
          placeholder: mchInfo.appSecret,
        },
        component: "InputTextArea",
      },
    ])
    mchInfo.appSecret = ""
    setFieldsValue({ ...mchInfo })
    // 隐藏底部时禁用整个表单
    setProps({ disabled: !showFooter })
  })
  const getTitle = computed(() => (!unref(isUpdate) ? "新增商户应用" : "编辑商户应用"))

  async function handleSubmit() {
    try {
      let values = await validate()
      setDrawerProps({ confirmLoading: true })
      values.userIdentity === 1 && (values.deptIds = "")
      await saveOrUpdateMchApp(values, unref(isUpdate))
      closeDrawer()
      createMessage.success(`${getTitle.value}成功`)
      emit("success", { isUpdate: unref(isUpdate), values: { ...values, id: rowId.value } })
    } finally {
      setDrawerProps({ confirmLoading: false })
    }
  }
</script>
